package com.web.user.member.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.base.user.entity.Member;
import com.web.user.member.ao.MemberAo;

import cn.mysteel.util.StringUtils;

@Controller
@RequestMapping("/member")
public class MemberAction
{
	@Autowired
	private MemberAo memberAo;
	
	@RequestMapping("/validate")
	public ModelAndView validate(HttpServletRequest request,HttpServletResponse response)
	{
		String email = request.getParameter ("email");
		String nickName = request.getParameter ("nickName");
		
		Map<String, Object> params = new HashMap<String,Object>();
		
		if(StringUtils.notTrimEmpty (email))
		{
			email = email.trim ();
			params.put ("email" ,email);
		}
		
		if(StringUtils.notTrimEmpty (nickName))
		{
			try
			{
				nickName = nickName.trim ();
				nickName = new String(nickName.getBytes("iso8859-1"),"UTF-8");
				
			}catch (UnsupportedEncodingException e)
			{
				e.printStackTrace();
			}
			params.put ("nickName" ,nickName);
		}
		
		String result = memberAo.validate (params);
		
		 response.setContentType("text/html;charset=gb2312");
		 PrintWriter out = null;
		try
		{
			out = response.getWriter();
		}catch (IOException e)
		{
			e.printStackTrace();
		}

		 out.print(result);
		 out.flush();
		 out.close();
		
		return null;
	}
	
	@RequestMapping("/register")
	public ModelAndView register(HttpServletRequest request,Member member,HttpServletResponse response)
	{
		memberAo.register (member);
		
		try
		{
			response.sendRedirect ("/member/reg_success.shtml?email="+member.getEmail  ());
			
		}catch (IOException e)
		{
			e.printStackTrace();
		}
		
		return null;
	}
	
	@RequestMapping ("/login")
	public ModelAndView login(HttpServletRequest request,HttpServletResponse response,String userName,String password)
	{
		
		Map<String, Object> params = new HashMap<String,Object>();
		
		params.put ("userName" ,userName.trim ());
		params.put ("password" ,password.trim ());
		params.put ("id" ,userName.trim ());
		params.put ("email" ,userName.trim ());
		
		Map<String, Object> map = memberAo.login (request ,params ,response);

		response.setContentType("text/html;charset=gb2312");
		PrintWriter out = null;
		try
		{
			out = response.getWriter();
		}catch (IOException e)
		{
			e.printStackTrace();
		}

		 out.print(map.get ("loginMessage"));
		 out.flush();
		 out.close();

		return null;
	}
	
	@RequestMapping("/logOut")
	public ModelAndView logOut(HttpServletRequest request,HttpServletResponse response,long memberId)
	{
		memberAo.logOut (request ,response ,memberId);
		
		return null;
	}
	
	@RequestMapping("/validateHashLogin")
	public ModelAndView validateHashLogin(long memberId,HttpServletRequest request,HttpServletResponse response)
	{
	/*	long id;
		if(!StringUtils.isTrimEmpty (memberId))
		{
			id = Long.parseLong (memberId);
		}*/
		
		
		String returnStr = memberAo.validateHashLogin (memberId ,request ,response);
		
		 response.setContentType("text/html;charset=gb2312");
		 PrintWriter out = null;
		try
		{
			out = response.getWriter();
		}catch (IOException e)
		{
			e.printStackTrace();
		}

		 out.print(returnStr);
		 out.flush();
		 out.close();
		
		return null;
	}
	@RequestMapping("/addGZ")
	public ModelAndView addGZ(HttpServletRequest request,long friend_id,HttpServletResponse response)
	{
	
		memberAo.addGz (friend_id ,request);
		
		
		response.setContentType("text/html;charset=gb2312");
		 PrintWriter out = null;
		try
		{
			out = response.getWriter();
		}catch (IOException e)
		{
			e.printStackTrace();
		}

		 out.print("success");
		 out.flush();
		 out.close();
		
		return null;
	}
	
	@RequestMapping("/addFriends")
	public ModelAndView addFriends(HttpServletRequest request,HttpServletResponse response,long friend_id)
	{
		memberAo.addFriends (request ,friend_id); 
		
		response.setContentType("text/html;charset=gb2312");
		 PrintWriter out = null;
		try
		{
			out = response.getWriter();
		}catch (IOException e)
		{
			e.printStackTrace();
		}

		 out.print("success");
		 out.flush();
		 out.close();
		
		return null;
	}
	
	@RequestMapping("/requestPass")
	public ModelAndView requestPass(HttpServletRequest request,long send_MId)
	{
		memberAo.requestPass(request, send_MId);
		
		return null;
	}
	
}
